package com.fita.admin.web.controller.data;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.search.data.VehicleParameterSearch;
import com.fita.admin.service.data.VehicleParameterService;
import com.fita.entity.data.VehicleParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 车辆参数管理
 *
 * @author cuteHU
 *
 */
@Controller
@RequestMapping("/vehicle-parameters")
@MenuSetting("menu-data")
public class VehicleParameterController {

    private Logger logger = LoggerFactory.getLogger(VehicleParameterController.class);

    @Autowired
    private VehicleParameterService vehicleParameterService;

    /**
     * list
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-vehicle-parameter-management")
    public ModelAndView list(Page page) {

        ModelAndView modelAndView = new ModelAndView("data/vehicle-parameter/vehicle-parameter-list");

        int totals = vehicleParameterService.count();
        if (totals > 0) {

            List<VehicleParameter> vehicleParameters = vehicleParameterService.listByPage(VehicleParameter.class, page.with(totals));

            modelAndView.addObject("vehicleParameters", vehicleParameters);
            modelAndView.addObject("page", page);

        }
        return modelAndView;

    }

    /**
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search() {
        return "data/vehicle-parameter/vehicle-parameter-search";
    }

    /**
     * @param search
     * @param page
     * @param request
     * @return
     */
    @MenuSetting("submenu-vehicle-parameter-management")
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    public ModelAndView onSearch(VehicleParameterSearch search, Page page, HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView("data/vehicle-parameter/vehicle-parameter-list");

        int totals = vehicleParameterService.countSearch(VehicleParameter.class, search.getPreparedFilter(false));
        if (totals > 0) {

            List<VehicleParameter> vehicleParameters = vehicleParameterService.search(VehicleParameter.class, search,
                    page.with(totals));
            modelAndView.addObject("vehicleParameters", vehicleParameters);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));

        }

        return modelAndView;

    }

    /**
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    @MenuSetting("submenu-vehicle-parameter-management")
    public ModelAndView create() {
        ModelAndView modelAndView = new ModelAndView("data/vehicle-parameter/vehicle-parameter-create");
        return modelAndView;
    }

    /**
     * @param vehicleParameter
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(VehicleParameter vehicleParameter) {

        HttpResult httpResult = new HttpResult();

        try {

            vehicleParameterService.createVehicleParameter(vehicleParameter);
            httpResult.success().message("新增车辆参数成功");

        } catch (BusinessException e) {
            httpResult.fail(e.getCode()).message(e.getMessage());
            logger.error("onCreate occurs error: ", e);

        } catch (Exception e) {
            logger.error("onCreate occurs error: ", e);
            httpResult.fail("500").message("新增车辆参数失败");
        }

        return httpResult;
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("data/vehicle-parameter/vehicle-parameter-edit");

        VehicleParameter vehicleParameter = vehicleParameterService.load(VehicleParameter.class, id);
        modelAndView.addObject("vehicleParameter", vehicleParameter);

        return modelAndView;
    }

    /**
     * @param vehicleParameter
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@PathVariable Long id, VehicleParameter vehicleParameter) {

        HttpResult httpResult = new HttpResult();
        try {
            vehicleParameterService.update(id, vehicleParameter);
            httpResult.success().message("修改成功");
        } catch (BusinessException e) {
            logger.error("onEdit occurs error: ", e);
            httpResult.fail(e.getCode()).message(e.getMessage());
        } catch (Exception e) {
            logger.error("#onEdit() - vehicleParameter team fail, exception is [{}]", e);
            httpResult.fail("500").message("修改失败");
        }
        return httpResult;
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/freeze", method = RequestMethod.GET)
    public ModelAndView freeze(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("data/vehicle-parameter/vehicle-parameter-freeze");
        modelAndView.addObject("id", id);
        return modelAndView;
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/freeze", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onFreeze(@PathVariable("id") Long id) {
        HttpResult result = new HttpResult();
        try {

            VehicleParameter vehicleParameter = vehicleParameterService.load(VehicleParameter.class, id);

            vehicleParameterService.freeze(vehicleParameter);

            result.success().message("冻结成功！");

        } catch (Exception e) {

            result.fail("500").message("冻结失败！");
        }

        return result;
    }

    /**
     * @param id
     * @return
     */

    @RequestMapping(value = "/{id}/activate", method = RequestMethod.GET)
    public ModelAndView activate(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("data/vehicle-parameter/vehicle-parameter-activate");
        modelAndView.addObject("id", id);
        return modelAndView;
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/activate", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onActivate(@PathVariable("id") Long id) {
        HttpResult result = new HttpResult();
        try {

            VehicleParameter vehicleParameter = vehicleParameterService.load(VehicleParameter.class, id);

            vehicleParameterService.activate(vehicleParameter);

            result.success().message("激活成功！");

        } catch (Exception e) {

            result.fail("500").message("激活失败！");
        }
        return result;
    }
}
